﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Daemon.Common.Extensions;

namespace Daemon.Common.Generator
{
    public class InputComponentGenerator : ComponentGenerator
    {
        public override string WidgetTypeName => "Input";

        public override ComponentResponse DoGenerate(Component component)
        {
            var response = new ComponentResponse();
            StringBuilder strResult = new StringBuilder();
            strResult.Append($"<Form.Item label='{component.Title}' name='{component.Name}' ");
            if (component.Required)
            {
                strResult.Append("rules={[{ required: true, message: '" + component.Message + "' }]}");
            }
            strResult.Append(" >");
            strResult.Append($"<{WidgetTypeName} value={{{component.Bind}}} />");
            strResult.Append($"</Form.Item>");
            response.ComponentHtml = strResult.ToString();
            response.VariableList = GenerateVariable(component);
            return response;
        }

        public override List<string> GenerateVariable(Component component)
        {
            var variableList = new List<string>();
            if (!string.IsNullOrEmpty(component.Bind))
            {
                variableList.Add($" const [{component.Bind},set{component.Bind.Capitalize()}] = useState<any>();");
            }
            return variableList;
        }
    }
}
